<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="userId">
        <el-input v-model="queryParams.userId" type="number" placeholder="用户ID" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="用户姓名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="更新日期" prop="dateRange">
        <el-date-picker v-model="dateRange" style="width: 320px" type="datetimerange" align="right"
          start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']"
          value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['management:management:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="managementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id " align="center" prop="id" /> -->
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="可提现金额" align="center" prop="withdrawableAmount" />
      <el-table-column label="冻结金额" align="center" prop="freezeAmount" />
      <el-table-column label="总金额" align="center" prop="totalAmount" />
      <el-table-column label="推荐人数" align="center" prop="recommenNum" />
      <el-table-column label="更新日期" align="center">
        <template slot-scope="scope">
          {{ scope.row.updateTime | timestampToDate }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['management:management:record']">佣金记录</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { timestampToDate } from '@/utils/time'

import {
  listManagement,
  getManagement,
  updateManagement
} from '@/api/finance/management'

export default {
  name: 'management',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 佣金管理表格数据
      managementList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      dateRange: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        userName: null,
        updateTime: null,
        ids: undefined
      },
      isSearch: false
    }
  },
  created() {
    this.getList()
  },

  filters: { timestampToDate },
  methods: {
    /** 查询佣金管理列表 */
    getList() {

      let params = {};
      if (this.isSearch) {
        if (this.dateRange && this.dateRange.length) {
          this.queryParams.params = {
            startTime: new Date(this.dateRange[0]).getTime() / 1000,
            endTime: new Date(this.dateRange[1]).getTime() / 1000
          }
        }

        params = Object.assign({}, this.queryParams);
      } else {
        params = {
          pageNum: this.queryParams.pageNum,
          pageSize: this.queryParams.pageSize
        }
      }


      this.loading = true
      listManagement(params).then((response) => {
        this.managementList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        userName: null,
        withdrawableAmount: null,
        freezeAmount: null,
        totalAmount: null,
        recommenNum: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.isSearch = true;
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.dateRange = [];
      this.queryParams.params = {};
      this.isSearch = false;
      this.queryParams.pageNum = 1;
      this.getList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加佣金管理'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {

      sessionStorage.setItem("row", JSON.stringify(row));
      this.$router.push({
        path: `/finance/management/record/${row.id}`,
      })

    },

    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.ids = this.ids;
      this.download(
        'management/management/export',
        {
          ...this.queryParams,
          ids: this.ids.length? this.ids : undefined
        },
        `management_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
